sheet = [
    {'name': '张三', 'password': '135384', 'balance': 5000},
    {'name': '李四', 'password': '684355', 'balance': 12000},
    {'name': '小明', 'password': '468979', 'balance': 636000}]


def find_account(name):
    """
    查找账户是否存在，存在则返回该账户对象编号，否则报错
    :param name:输入的账户名
    :return:被找到账户的对象编号:
    """
    for i in range(len(sheet)):
        if sheet[i]['name'] == name:
            return i
    else:
        return -1


def show_balance(name):
    """
    查询余额函数
    :param name: 输入的账户
    """
    print('户主:', name, ' 当前余额为：', sheet[find_account(name)]['balance'], '元')


def deposit(name, money=0):
    """
    存款函数
    :param name: 输入的账户
    :param money: 存入的钱数
    """
    if money > 0:
        sheet[find_account(name)]['balance'] += money
        print('户主:', name, '存入', money, '元^_^\n当前余额为：', sheet[find_account(name)]['balance'], '元')
    else:
        print('金额输入错误，请重新操作！')


def withdraw(name, money=0):
    """
    取款函数
    :param name: 输入的账户
    :param money: 取出的钱数
    """
    if 0 < money <= sheet[find_account(name)]['balance']:
        sheet[find_account(name)]['balance'] -= money
        print('户主:', name, '取出', money, '元\n当前余额为：', sheet[find_account(name)]['balance'], '元')
    elif money > sheet[find_account(name)]['balance']:
        print('当前账户余额不足，仅有', sheet[find_account(name)]['balance'], '元!!! 请重新操作~')
    else:
        print('金额输入错误，请重新操作！')


def create_account():
    """
    创建账户
    :return:
    """
    name = input('请输入新户主姓名：')
    if len(name) != 0:
        while True:
            password = input('请设置密码（6位数字）：')
            for i in password:
                if ord(i) > 57 or ord(i) < 48:
                    print('密码仅能设置数字！  请重新输入~')
                    break
            else:
                break
        account = {'name': name, 'password': password, 'balance': 0}
        sheet.append(account)
        print("户主:", name, '创建账户成功！')
    else:
        print('姓名不能为空！请重新操作~')


def del_account():
    """
    注销账户
    :return:
    """
    name = input('请输入待注销的户主姓名：')
    if find_account(name) < 0:
        print('该账户不存在!请重新操作~')
        return

    password = input('请输入账户密码：')
    if password != sheet[find_account(name)]['password']:
        print('密码错误！请重新操作~')
        return

    print('户主:', name, ' 当前余额为：', sheet[find_account(name)]['balance'], '元\n确认注销该账户？(按\'Y/N\'(确认/取消))')
    while a := input():
        if a == 'n' or a == 'N':
            print('注销取消~')
            return
        elif a == 'y' or a == 'Y':
            break
        else:
            print('输入无效，请重新输入：')

    del sheet[find_account(name)]
    print('成功注销账户~')


while True:
    print('=' * 12, '自动取款机', '=' * 12)
    print('{:1}{:<12}{:<12}'.format(' ', '1、账户登录', '2、注册账户'))
    print('{:1}{:<12}{:<12}'.format(' ', '3、退出系统', ' '))
    print('=' * 34)
    key1 = input('请选择对应的操作：')
    if key1 == '1':
        _name = input('请输入账户名：')
        if (_account := find_account(_name)) >= 0:
            _password = input('请输入密码：')
            if _password == sheet[_account]['password']:
                print('登录成功！\n', '=' * 34)
                while True:
                    print('=' * 12, '自动取款机', '=' * 12)
                    print('{:1}{:<12}{:<12}'.format(' ', '1、存款', '2、取款'))
                    print('{:1}{:<12}{:<12}'.format(' ', '3、余额查询', '4、注销账户'))
                    print('{:1}{:<12}{:12}'.format(' ', '5、退出登录', ' '))
                    print('=' * 34)
                    key2 = input('请选择对应的操作：')
                    if key2 == '1':
                        deposit(_name, int(input('请输入存款金额：')))
                        print('=' * 34)
                    elif key2 == '2':
                        withdraw(_name, int(input('请输入取款金额：')))
                        print('=' * 34)
                    elif key2 == '3':
                        show_balance(_name)
                        print('=' * 34)
                    elif key2 == '4':
                        del_account()
                        print('=' * 34)
                        break
                    elif key2 == '5':
                        print("成功退出登录！")
                        print('=' * 34)
                        break
                    else:
                        print('无效操作！请重新输入~')
            else:
                print('密码输入错误！请重新登录~')
        else:
            print('该账户不存在！请重新登录~')
    elif key1 == '2':
        print('=' * 34)
        create_account()
    elif key1 == '3':
        print('=' * 34)
        print('成功退出系统！')
        break
    else:
        print('无效操作！请重新输入~')





